// pages/spray/deviceLog.js
const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    status: 1,
    statusList: [{
      label: '正常',
      key: 'normal',
      value: 1
    },
    {
      label: '异常',
      key: 'error',
      value: 3
    }],
    icontype : "",
    page : 1,
    haveNext: false,
    dataList: [],
    deviceId : '',
    deviceInfo : {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin,
      icontype : options.icontype || '',
      deviceId : options.deviceId || ''
    })
    this.loadList(1);
    if ('dianshifa' == this.data.icontype) { //电池阀，请求次数
      this.getDeviceInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.haveNext) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadList(this.data.page);
    }
  },
  loadList: function (page) {
    let that = this;
    let getListParams = {
        "deviceId" : that.data.deviceId,
        "page": page,
        "pageSize": 15,
        "notQasIsNormal" : this.data.status == 3 ? 0 : 1
      };
    utils.promiseGet('/wx/device/spray/getList', getListParams).then(res => {
      let list = that.data.dataList;
      let haveNext = that.data.haveNext;
      if (page == 1) {
        list = [];
      }
      if (res.code == 200) {
        let result = res.data;
        if (result.rows.length > 0) {
          for (let i = 0; i < result.rows.length; i++) {
            let itemRow = {
              'id' : result.rows[i].psdId,
              'time' : result.rows[i].updateTime,
              'content' : '电池阀、电源、通信、感应模块无异常'
            };
            itemRow.content = that.formatWorkStatus(result.rows[i].exceptionType, result.rows[i].plateStatus);
            list.push(itemRow);
          }
          haveNext = true;
        } else {
          haveNext = false;
        }
      } else {
      }
      this.setData({
        dataList: list,
        haveNext: haveNext
      });
    }).catch(e => {
      console.log(e);
    });
  },
  formatWorkStatus(exceptionType, plateStatus) {
    let strArr = [];
    if ((exceptionType >> 11) & 1 == 1) {
      strArr.push('24V电源接入状态');
    }
    if ((exceptionType >> 12) & 1 == 1) {
      strArr.push('电磁阀工作状态');
    }
    if ((exceptionType >> 13) & 1 == 1) {
      strArr.push('感应器工作状态');
    }
    if ((exceptionType >> 14) & 1 == 1) {
      strArr.push('温湿度传感器工作状态');
    }
    if ((exceptionType >> 15) & 1 == 1) {
      strArr.push('气体传感器工作状态');
    }
    if (plateStatus == 1) {
      strArr.push('从板未连接或从板无响应');
    }
    if (plateStatus == 2) {
      strArr.push('从板通信异常(数据错误、丢包)');
    }
    return strArr.join('/') + "异常";
  },
  onChangeTab(e) {
    
    const { status } = e.currentTarget.dataset
    this.setData({ status })
    this.setData({
      page: 1
    });
    this.loadList(1);
  },

  toPageAnalysis() {
    wx.navigateTo({
      url: '/pages/spray/eleAnalysis?deviceId=' + this.data.deviceId,
    })
  },
  getDeviceInfo : function() {
    let that = this;
    let p1 = utils.promiseGet('/wx/device/spray/getDeviceInfoById', {"deviceId" : this.data.deviceId});
    Promise.all([p1]).then((result) => {
      console.log(result);
      //p1
      if (result && result[0] && result[0].code == 200) {
        let deviceInfo = result[0].data || {};
        that.setData({
          deviceInfo : deviceInfo
        });
      }
    }).catch(function(e){
        console.log(e);
    });
  }
})